package ru.amse.podolsky.fractalarch.io;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Алексей
 */

//package ru.amse.podolsky.fractalarch;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class ImagesIO {

    private final File path;
    
    private final BufferedImage buff;
    
    public ImagesIO(String s) {
        this.path = new File(s);
        this.buff = null;
    }
    
    public BufferedImage read() throws IOException{
        return ImageIO.read(this.path);
    }
    
    public void write(BufferedImage buff) throws IOException{
       javax.imageio.ImageIO.write(buff, "bmp", this.path);
    }

    public void writeFormated(BufferedImage buff) throws IOException{
       String format = getFormat(this.path.getAbsolutePath());
       javax.imageio.ImageIO.write(buff, format, this.path);
    }

    private String getFormat(String path) {
        int length = path.length();

        if(path.length() == 0){
            return "";
        }

        char[] ch = path.toCharArray();
        char[] newch = new char[length];
        int i = 1;
        while(ch[length - i] != '.'){
            newch[i] = ch[length - i];
            i++;
        }

        StringBuilder result = new StringBuilder();

        if(i == 4){
             result.append(newch[3]);
             result.append(newch[2]);
             result.append(newch[1]);
        }else{
            result.append(newch[4]);
            result.append(newch[3]);
            result.append(newch[2]);
            result.append(newch[1]);
        }

        String res = result.toString();

        return res;
    }
}
